package com.road7.mina.protobufmsg;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IoSession;

import com.road7.mina.pb.student.StudentMsgProto;
import com.road7.mina.pb.student.StudentMsgProto.StudentRspMsg;

/**
 * @Description :
 * @FileName: AbstractHandler.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年11月23日 下午8:32:28
 * @Version:V1.00
 */
public abstract class AbstractHandler extends IoHandlerAdapter {

	private String which;

	private int i;

	public AbstractHandler(String which) {
		this.which = which;
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {

		// 读取客户端传过来的Student对象
		StudentMsgProto.StudentRspMsg student = (StudentMsgProto.StudentRspMsg) message;

		System.out.println(student);

		if ("client".equalsIgnoreCase(which)) {
			if (i++ == 5) {
				session.close(true);
				return;
			}
		}
		if ("server".equalsIgnoreCase(which)) {
			i++;
		}
		StudentMsgProto.StudentRspMsg.Builder builder = StudentMsgProto.StudentRspMsg.newBuilder();

		StudentRspMsg studentRspMsg = builder.setId(i).setName("test").setEmail("15590@qq.com").addFriends("X")
				.addFriends("y").build();

		session.write(studentRspMsg);
	}

}